/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmDangNhap.java
 *
 * Created on Aug 24, 2012, 8:48:55 AM
 */
package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import pojo.*;
import dao.*;
import org.hibernate.HibernateException;

/**
 *
 * @author dtth
 */
public class frmDangNhap extends javax.swing.JFrame {

    // Variables declaration - do not modify
    
    private JLabel jlblTieuDe = new JLabel("ĐĂNG NHẬP", JLabel.RIGHT);
    private JLabel jlblDangNhap = new JLabel("Tên đăng nhập: ");
    private JTextField jtxtTenDangNhap = new JTextField();
    private JLabel jlblMatKhau = new JLabel("Mật khẩu: ");
    private JPasswordField jpasMatKhau = new JPasswordField();
    private JButton jbtnDangNhap = new JButton("Đăng nhập");
    private JButton jbtnThoat = new JButton("Thoát");
    
    GridBagConstraints cons = new GridBagConstraints();
    GridLayout mainLayout = new GridLayout(2, 2);
    // End of variables declaration
    
    /** Creates new form frmDangNhap */
    public void addGB(Component component, int x, int y ) {
        cons.gridx = x;
        cons.gridy = y;
        add ( component, cons );
    }
    
    public void addListen() {
        jbtnDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jbtnDangNhapActionPerformed(evt);
                } catch (Exception ex) {
                    Logger.getLogger(frmDangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });
    }
    private void jbtnDangNhapActionPerformed(java.awt.event.ActionEvent evt) throws Exception {
        // TODO add your handling code here:
        try {
            String strTenDangNhap = this.jtxtTenDangNhap.getText();
            String strMatKhau = this.jpasMatKhau.getText();
            Nhanvien nv = new Nhanvien(strTenDangNhap, strMatKhau);
            if(NhanVienDAO.KiemTraDangNhap(nv)==true) {
                //JOptionPane.showMessageDialog(this, "Đăng nhập thành công.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
                frmMain main = new frmMain();
                main.setVisible(true);
            }
            else
                JOptionPane.showMessageDialog(this, "Sai mật khẩu hoặc tên đăng nhập. Vui lòng nhập lại.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, "Loi: " + ex.toString(), "Thông báo", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        int n = JOptionPane.showConfirmDialog(this,"Bạn có muốn thoát không?",
                                "Thông báo", JOptionPane.YES_NO_OPTION);
        if(n==0)
            this.dispose();
    }
    public frmDangNhap() {
        initComponents();
        this.setSize(358, 150);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension di = tk.getScreenSize();
        this.setLocation(di.width/3, di.height/3);
        addListen();
        
        setLayout(new GridBagLayout());
        
        this.jlblTieuDe.setFont(new java.awt.Font("Tahoma", 1, 14));
        
        cons.fill = GridBagConstraints.BOTH;
        
        
        cons.weighty = 5.0;
        cons.weightx = 0.3;
        cons.gridwidth = 2;
        addGB(jlblTieuDe, 0, 0);
        
        cons.weighty = 1;
        cons.weightx = 1;
        addGB(jlblDangNhap, 1, 1);
        addGB(jlblMatKhau, 1, 2);
        
        addGB(jtxtTenDangNhap, 2, 1);
        addGB(jpasMatKhau, 2, 2);
        
        addGB(jbtnDangNhap, 1, 4);
        addGB(jbtnThoat, 2, 4);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 271, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 159, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmDangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmDangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmDangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmDangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmDangNhap().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
